﻿using BaoShouTang.DBModel;
using BaoShouTangServer.ApiServies;
using BaoShouTangServer.DTOModel;
using BaoShouTangServer.Models;
using BaoShouTangServer.Service;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using U8Login;

namespace BaoShouTangServer.Controllers
{
    public class StockInController : Controller
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();


        public JsonResult StockIn(Guid 实际产出guid)
        {
            Logger.Warn("StockIn接受到的当行成品入库guid：" + JsonConvert.SerializeObject(实际产出guid));
            string resp = "";
            string msg = "";
            string responseMsg = "";

            HttpData<List<DTOU8材料出库单导入结果>> httpData = new HttpData<List<DTOU8材料出库单导入结果>>();

            List<Z_任务单实际产出> z_任务单实际产出List = SqlSugarHelp.scope.Queryable<Z_任务单实际产出>().Where(it => it.实际产出guid == 实际产出guid).ToList();
            if (z_任务单实际产出List != null && z_任务单实际产出List.Count > 0)
            {
                foreach (var z_任务单实际投入 in z_任务单实际产出List)
                {
                    if (z_任务单实际投入.U8入库单id.HasValue == true)
                    {
                        rdrecords10 rdrecordS10 = SqlSugarHelp.scope.Queryable<rdrecords10>().Where(it => it.cDefine23 == z_任务单实际投入.实际产出guid.ToString()).First();
                        if (rdrecordS10 != null)
                        {
                            rdrecord10 rdrecord10 = SqlSugarHelp.scope.Queryable<rdrecord10>().Where(it => it.ID == rdrecordS10.ID).First();
                            httpData.msg = $"{z_任务单实际投入.产出物料编号}已经入库了，入库单号为{rdrecord10.cCode}";
                            return Json(httpData);
                        }
                    }
                }
                //z_任务单实际投入List 按仓库分组
                var groupBy = z_任务单实际产出List.GroupBy(it => it.仓库编号);

                clsLogin clsLogin = LogInService.GetLogin(ref resp, ref msg);
                if (resp != "登陆成功")
                {
                    return Json(new { success = false, message = $"{msg}" });
                }

                //遍历分组
                foreach (var item in groupBy)
                {
                    //获取仓库编号
                    string 仓库编号 = item.Key;
                    //获取该仓库下的所有发料记录
                    List<Z_任务单实际产出> z_任务单实际产出ListBy仓库 = item.ToList();
                    //开始生成U8出库单
                    string 到货单接口返回result = "";
                    string rdrecord10Id = "";
                    bool result = rdrecord10Service.生成成品入库单(clsLogin, z_任务单实际产出ListBy仓库, ref rdrecord10Id, ref 到货单接口返回result);
                    string 物料号 = string.Join(",", z_任务单实际产出ListBy仓库.Select(it => it.产出物料编号).ToArray());
                    if (result == true)
                    {
                        rdrecord10 rdrecord10已推送 = SqlSugarHelp.scope.Queryable<rdrecord10>().Where(it => it.ID == int.Parse(rdrecord10Id)).First();
                        foreach (var 任务单实际产出已推送 in z_任务单实际产出ListBy仓库)
                        {
                            任务单实际产出已推送.U8入库单id = int.Parse(rdrecord10Id);
                            任务单实际产出已推送.U8入库单号 = rdrecord10已推送.cCode;
                            任务单实际产出已推送.U8入库状态 = "入库成功";
                            任务单实际产出已推送.U8入库失败原因 = "";
                            任务单实际产出已推送.U8入库日期 = rdrecord10已推送.dDate;
                            httpData.msg += $@"{仓库编号}仓库：{物料号}入库成功。入库单号：{rdrecord10已推送.cCode}\r\n";
                        }
                    }
                    else
                    {
                        foreach (var 任务单实际产出已推送 in z_任务单实际产出ListBy仓库)
                        {
                            任务单实际产出已推送.U8入库失败原因 = 到货单接口返回result;
                            任务单实际产出已推送.U8入库状态 = "入库失败";
                            httpData.msg += $@"{仓库编号}仓库：[{物料号}]入库失败,原因为:[{到货单接口返回result}]\r\n";
                        }
                    }
                }
                int r = SqlSugarHelp.scope.Updateable(z_任务单实际产出List).ExecuteCommand();
            }

            return Json(httpData);
        }

        public JsonResult StockInList(List<Guid> 实际产出guidList)
        {
            Logger.Warn("StockInList接受到的当行成品入库guid：" + JsonConvert.SerializeObject(实际产出guidList));
            string resp = "";
            string msg = "";
            string responseMsg = "";

            HttpData<List<DTOU8材料出库单导入结果>> httpData = new HttpData<List<DTOU8材料出库单导入结果>>();

            List<Z_任务单实际产出> z_任务单实际产出List = SqlSugarHelp.scope.Queryable<Z_任务单实际产出>().Where(it => 实际产出guidList.Contains(it.实际产出guid)).ToList();
            if (z_任务单实际产出List != null && z_任务单实际产出List.Count > 0)
            {
                foreach (var z_任务单实际投入 in z_任务单实际产出List)
                {
                    if (z_任务单实际投入.U8入库单id.HasValue == true)
                    {
                        rdrecords10 rdrecordS10 = SqlSugarHelp.scope.Queryable<rdrecords10>().Where(it => it.cDefine23 == z_任务单实际投入.实际产出guid.ToString()).First();
                        if (rdrecordS10 != null)
                        {
                            rdrecord10 rdrecord10 = SqlSugarHelp.scope.Queryable<rdrecord10>().Where(it => it.ID == rdrecordS10.ID).First();
                            httpData.msg = $"{z_任务单实际投入.产出物料编号}已经入库了，入库单号为{rdrecord10.cCode}";
                            return Json(httpData);
                        }
                    }
                }
                //z_任务单实际投入List 按仓库分组
                var groupBy = z_任务单实际产出List.GroupBy(it => it.仓库编号);

                clsLogin clsLogin = LogInService.GetLogin(ref resp, ref msg);
                if (resp != "登陆成功")
                {
                    return Json(new { success = false, message = $"{msg}" });
                }

                //遍历分组
                foreach (var item in groupBy)
                {
                    //获取仓库编号
                    string 仓库编号 = item.Key;
                    //获取该仓库下的所有发料记录
                    List<Z_任务单实际产出> z_任务单实际产出ListBy仓库 = item.ToList();

                    var groupBy入库类别 = z_任务单实际产出ListBy仓库.GroupBy(it => it.入库类别);

                    foreach (var item入库类别 in groupBy入库类别)
                    {
                        List<Z_任务单实际产出> z_任务单实际产出ListBy入库类别 = item入库类别.ToList();
                        //开始生成U8出库单
                        string 到货单接口返回result = "";
                        string rdrecord10Id = "";
                        bool result = rdrecord10Service.生成成品入库单(clsLogin, z_任务单实际产出ListBy入库类别, ref rdrecord10Id, ref 到货单接口返回result);
                        string 物料号 = string.Join(",", z_任务单实际产出ListBy入库类别.Select(it => it.产出物料编号).ToArray());
                        if (result == true)
                        {
                            rdrecord10 rdrecord10已推送 = SqlSugarHelp.scope.Queryable<rdrecord10>().Where(it => it.ID == int.Parse(rdrecord10Id)).First();
                            foreach (var 任务单实际产出已推送 in z_任务单实际产出ListBy入库类别)
                            {
                                任务单实际产出已推送.U8入库单id = int.Parse(rdrecord10Id);
                                任务单实际产出已推送.U8入库单号 = rdrecord10已推送.cCode;
                                任务单实际产出已推送.U8入库状态 = "入库成功";
                                任务单实际产出已推送.U8入库失败原因 = "";
                                任务单实际产出已推送.U8入库日期 = rdrecord10已推送.dDate;
                            }
                            httpData.msg += $@"{仓库编号}仓库：{物料号}入库成功。入库单号：{rdrecord10已推送.cCode}\r\n";

                        }
                        else
                        {
                            foreach (var 任务单实际产出已推送 in z_任务单实际产出ListBy入库类别)
                            {
                                任务单实际产出已推送.U8入库失败原因 = 到货单接口返回result;
                                任务单实际产出已推送.U8入库状态 = "入库失败";
                            }
                            httpData.msg += $@"{仓库编号}仓库：[{物料号}]入库失败,原因为:[{到货单接口返回result}]\r\n";

                        }
                        int r = SqlSugarHelp.scope.Updateable(z_任务单实际产出ListBy入库类别).ExecuteCommand();
                    }

                        
                }
               
            }

            return Json(httpData);
        }
    }
}